﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using COFE.Core.Validater;
using NaturalSorting;

namespace COFE.Core
{
    public class PropertyPairComparer : IComparer<PropertyPair>
    {
        private static NaturalComparer naturalComparer = new NaturalComparer();

        public PropertyPairComparer(object property)
        {
            Property = property;
            PropertyType = PropertyTypeExtension.GetDefaultPropertyType(property);
        }

        public object Property { get; private set; }
        public Type PropertyType { get; private set; }

        public int Compare(PropertyPair x, PropertyPair y)
        {
            if (x.Value == null)
                return 1;
            if (y.Value == null)
                return -1;

            switch ((int)Property)
            {                
                case (int)FileSystemInfoProperties.FullParseName :
                case (int)FileSystemInfoProperties.Name:
                case (int)FileSystemInfoProperties.Label:
                    return naturalComparer.Compare(x.ValueAsString, y.ValueAsString);
            }

            switch (PropertyType.ToString())
            {
                case "System.Int32" :
                    return x.ValueAsInt32.CompareTo(y.ValueAsInt32);
                case "System.Int64":
                    return x.ValueAsInt64.CompareTo(y.ValueAsInt64);
                case "System.DateTime" :
                    return x.ValueAsLocalDateTime.CompareTo(y.ValueAsLocalDateTime);
                default :
                    return x.Value.ToString().CompareTo(y.Value.ToString());
            }

        }
    }
}
